﻿#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <list>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib/BaseLib_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Error.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_APIHelp/APIHelp_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_APIHelp/APIHelp_Error.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetHelp_XRpc.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetHelp_APIHelp.lib")

void __stdcall XRPCClient_Callback(XNETHANDLE xhToken, LPCTSTR lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE nRtnType, NETHELP_XRPCPARAMETE* pSt_XRPCParamete, LPVOID lParam)
{

}
XNETHANDLE xhToken;
int XEngine_AddMethod(int a, int b)
{
	list<NETHELP_XRPCPARAMETE> stl_ListParamete;

	NETHELP_XRPCPARAMETE st_XPRCReturn;
	memset(&st_XPRCReturn, '\0', sizeof(NETHELP_XRPCPARAMETE));

	st_XPRCReturn.lParameteValue = malloc(1000);
	memset(st_XPRCReturn.lParameteValue, '\0', 1000);

	NetHelp_XRPCHelp_Malloc(&stl_ListParamete, 2);

	list<NETHELP_XRPCPARAMETE>::iterator stl_ListIterator = stl_ListParamete.begin();
	stl_ListIterator->enXRPC_ParameteType = ENUM_NETHELP_XRPC_PARAMETE_TYPE_INT;
	stl_ListIterator->nParameteLen = sizeof(int);
	memcpy(stl_ListIterator->lParameteValue, &a, sizeof(int));

	stl_ListIterator++;
	stl_ListIterator->enXRPC_ParameteType = ENUM_NETHELP_XRPC_PARAMETE_TYPE_INT;
	stl_ListIterator->nParameteLen = sizeof(int);
	memcpy(stl_ListIterator->lParameteValue, &b, sizeof(int));

	LPCTSTR lpszFuncName = _T("XEngine_AddMethod");
	if (!NetHelp_XRPCClient_ReqFuncCall(xhToken, lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE_INT, &stl_ListParamete, &st_XPRCReturn))
	{
		printf("NetHelp_XRPCClient_ReqFuncCall1:%lX\n", XRpc_GetLastError());
		return 0;
	}
	NetHelp_XRPCHelp_Free(&stl_ListParamete);
	stl_ListParamete.clear();

	int nRet = 0;
	memcpy(&nRet, st_XPRCReturn.lParameteValue, sizeof(int));
	return nRet;
}
void XEngine_Print(LPCTSTR lpszString)
{
	list<NETHELP_XRPCPARAMETE> stl_ListParamete;

	NETHELP_XRPCPARAMETE st_XPRCReturn;
	memset(&st_XPRCReturn, '\0', sizeof(NETHELP_XRPCPARAMETE));

	st_XPRCReturn.lParameteValue = malloc(1000);
	memset(st_XPRCReturn.lParameteValue, '\0', 1000);

	NetHelp_XRPCHelp_Malloc(&stl_ListParamete, 1);

	list<NETHELP_XRPCPARAMETE>::iterator stl_ListIterator = stl_ListParamete.begin();
	stl_ListIterator->enXRPC_ParameteType = ENUM_NETHELP_XRPC_PARAMETE_TYPE_STRING;
	stl_ListIterator->nParameteLen = _tcslen(lpszString);
	memcpy(stl_ListIterator->lParameteValue, lpszString, stl_ListIterator->nParameteLen);

	LPCTSTR lpszFuncName = _T("XEngine_Print");
	if (!NetHelp_XRPCClient_ReqFuncCall(xhToken, lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE_VOID, &stl_ListParamete))
	{
		printf("NetHelp_XRPCClient_ReqFuncCall2:%lX\n", XRpc_GetLastError());
		return;
	}
	NetHelp_XRPCHelp_Free(&stl_ListParamete);
	stl_ListParamete.clear();
}
int XEngine_HttpAdd(int a, int b)
{
	int nCode = 0;
	int nLen = 1024;
	TCHAR tszMsgBuffer[1024];
	TCHAR tszFileBuffer[1024];
	LPCTSTR lpszHttpAddr = _T("http://127.0.0.1:50002/XRPCApi/FuncCall");
	LPCTSTR lpszJsonFile = _T("G:\\netengineapp\\NetEngine_WINApps\\Debug\\1.txt");

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	memset(tszFileBuffer, '\0', sizeof(tszFileBuffer));

	FILE* pSt_File = _tfopen(lpszJsonFile, "rb");
	int nRet = fread(tszFileBuffer, 1, sizeof(tszFileBuffer), pSt_File);
	fclose(pSt_File);

	APIHelp_HttpRequest_Post(lpszHttpAddr, &nCode, tszFileBuffer, tszMsgBuffer, &nLen);
	printf("%s\n", tszMsgBuffer);
	return nRet;
}
int main()
{
	WSADATA st_WSAData;
	memset(&st_WSAData, '\0', sizeof(WSADATA));
	WSAStartup(MAKEWORD(2, 2), &st_WSAData);

	LPCTSTR lpszAddr = _T("127.0.0.1");
	if (!NetHelp_XRPCClient_Connect(&xhToken, lpszAddr, 50001))
	{
		printf("NetHelp_XRPCClient_Connect\n");
	}

	printf("XEngine_AddMethod(1, 2) = %d\n", XEngine_AddMethod(1, 2));
	XEngine_Print("asdasdasd");
	printf("XEngine_Print\n");

	XEngine_HttpAdd(1, 2);
	printf("wandan\n");
	while (1)
	{
		Sleep(1000);
	}
	WSACleanup();

	return 0;
}